package com.mojo.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 基于注解的声明式事务实现
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2021, 广州九尾信息科技有限公司
 * @date 2021/6/21 14:17
 */
@Service
public class UserDaoImpl_Annotation implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //@Autowired
    //private PlatformTransactionManager transactionManager;

    @Transactional(rollbackFor = Exception.class,
            //隔离级别
            isolation = Isolation.READ_COMMITTED
            //传播行为
            , propagation = Propagation.REQUIRED,
            timeout = 30)
    @Override
    public int update(Long id) {
        int update = 0;
        String sql = "update backstage_user set nickname= ? where id=?";
        Object[] objects = {"牧哲", 1};
        update = jdbcTemplate.update(sql, objects);
        String insert = "insert into backstage_role (name,description) values(?,?)";
        Object[] objects1 = {"超管", "超管职位"};
        int insert1 = jdbcTemplate.update(insert, objects1);
        //try {
        //    String sql = "update backstage_user set nickname= ? where id=?";
        //    Object[] objects = {"mojo11111", 1};
        //    update = jdbcTemplate.update(sql, objects);
        //    String insert = "insert into backstage_role (name,description) values(?,?)";
        //    Object[] objects1 = {"超管"};
        //    int insert1 = jdbcTemplate.update(insert, objects1);
        //} catch (DataAccessException e) {
        //    e.printStackTrace();
        //}
        return update;
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRES_NEW)
    public void updateById(Long id) {
        String sql = "update backstage_user set nickname= ? where id=?";
        Object[] objects = {"牧哲", 1};
        int update = jdbcTemplate.update(sql, objects);


    }
}
